<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将数组的内部指针指向第一个单元</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.range.html">« range</a></li>
      <li style="float: right;"><a href="function.rsort.html">rsort »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>将数组的内部指针指向第一个单元</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.reset" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">reset</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">reset</span> &mdash; <span class="dc-title">将数组的内部指针指向第一个单元</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.reset-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>reset</strong></span>(<span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">object</span></span> <code class="parameter reference">&$array</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>reset()</strong></span> 将 <code class="parameter">array</code>
   的内部指针倒回到第一个单元并返回第一个数组单元的值。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.reset-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       输入的数组。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.reset-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回数组第一个单元的值，如果数组为空则返回 <strong><code>false</code></strong>。
  </p>
  <div class="warning"><strong class="warning">警告</strong><p class="simpara">此函数可能返回布尔值
<strong><code>false</code></strong>，但也可能返回等同于 <strong><code>false</code></strong> 的非布尔值。请阅读 <a href="language.types.boolean.html" class="link">布尔类型</a>章节以获取更多信息。应使用
<a href="language.operators.comparison.html" class="link">===
运算符</a>来测试此函数的返回值。</p></div>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.reset-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.1.0</td>
 <td>
  弃用在 <span class="type">object</span> 上调用此函数。
  在 <span class="type">object</span> 优先使用 <span class="function"><a href="function.get-mangled-object-vars.html" class="function">get_mangled_object_vars()</a></span> 或者使用 <span class="classname"><a href="class.arrayiterator.html" class="classname">ArrayIterator</a></span>。
 </td>
</tr>


    </tbody>
   
  </table>

 </div>

 
 <div class="refsect1 examples" id="refsect1-function.reset-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5007">
    <p><strong>示例 #1 <span class="function"><strong>reset()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'step&nbsp;one'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'step&nbsp;two'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'step&nbsp;three'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'step&nbsp;four'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;默认情况下，指针指向第一个元素<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">current</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;"step&nbsp;one"<br /><br />//&nbsp;跳过两步<br /></span><span style="color: #0000BB">next</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">next</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">current</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;"step&nbsp;three"<br /><br />//&nbsp;重置指针，重新指向第一个元素<br /></span><span style="color: #0000BB">reset</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">current</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;"step&nbsp;one"<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.reset-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    返回的值无法区分是空数组，还是第一个元素是 <span class="type">bool</span> <strong><code>false</code></strong>。
    要正确检测数组第一个元素包含 <strong><code>false</code></strong> 的情况，首先要检测数组 <span class="function"><a href="function.count.html" class="function">count()</a></span>，
    或在调用 <span class="function"><strong>reset()</strong></span> 后检测 <span class="function"><a href="function.key.html" class="function">key()</a></span> 不为 <strong><code>null</code></strong>。
   </span>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.reset-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.current.html" class="function" rel="rdfs-seeAlso">current()</a> - 返回数组中的当前值</span></li>
    <li class="member"><span class="function"><a href="function.each.html" class="function" rel="rdfs-seeAlso">each()</a> - 返回数组中当前的键／值对并将数组指针向前移动一步</span></li>
    <li class="member"><span class="function"><a href="function.end.html" class="function" rel="rdfs-seeAlso">end()</a> - 将数组的内部指针指向最后一个单元</span></li>
    <li class="member"><span class="function"><a href="function.next.html" class="function" rel="rdfs-seeAlso">next()</a> - 将数组中的内部指针向前移动一位</span></li>
    <li class="member"><span class="function"><a href="function.prev.html" class="function" rel="rdfs-seeAlso">prev()</a> - 将数组的内部指针倒回一位</span></li>
    <li class="member"><span class="function"><a href="function.array-key-first.html" class="function" rel="rdfs-seeAlso">array_key_first()</a> - 获取指定数组的第一个键</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>